﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class student_myScore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //只显示学生选了课的考试
        string course_id = "";
        string course_name = "";
        //Session["student_id"] = "3120182109222";
        System.Data.DataTable table1 = new System.Data.DataTable();
        int count = 0;//总记录的条数

        try
        {
            //填充数据
            int page = Int32.Parse(Request.QueryString["page"].ToString().Trim());
            int limit = Int32.Parse(Request.QueryString["limit"].ToString().Trim());
            if (Request.QueryString["course_id"] != null)
            {
                course_id = Request.QueryString["course_id"].ToString().Trim();
            }
            if (Request.QueryString["course_name"] != null)
            {
                course_name = Request.QueryString["course_name"].ToString().Trim();
            }
            
            string sqlGetDataCount = "select count(*) from tb_score  "
                + " where student_id = '" + Session["student_id"]+"'";

            List<string> wheres = new List<string>();
            List<string> wheres1 = new List<string>();        //为搜索服务的
            if (course_id != "")
            {
                wheres.Add("course_id like '%" + course_id + "%'");
                wheres1.Add("b.course_id like '%" + course_id + "%'");
            }
            if (course_name != "")
            {
                wheres.Add("course_name like '%" + course_name + "%'");
                wheres1.Add("b.course_name like '%" + course_name + "%'");
            }
            string wh = "";
            if (wheres.Count > 0)
            {
                wh = string.Join(" and ", wheres.ToArray());
                sqlGetDataCount = sqlGetDataCount 
                    + " and   course_id in (select course_id from tb_course where  " 
                    + wh +")";
            }
            count = Int32.Parse(SqlServer.ExecuteScalar(sqlGetDataCount));
            int page_temp = page;
            int limit_temp = limit;
            if (count < page * limit)
            {
                limit = count - (page - 1) * limit;
            }
            page = (page_temp - 1) * limit_temp;
            string wh1 = "";
            if (wheres1.Count > 0)
            {
                wh1 = string.Join(" and ", wheres1.ToArray());
                sqlGetDataCount = sqlGetDataCount + " and " + wh;
            }
            string sql = "select test_question_id,c.test_question_name, a.course_id,b.course_name ,a.test_score from tb_score as a inner join  tb_course as b on a.course_id = b.course_id "
                        +" inner join tb_test_questions as c on b.course_id =c.course_id where student_id  =   '" + Session["student_id"] +"'" ;
            if (wheres.Count > 0)
            {
                sql = sql + " and  " + wh1 + " and a.course_id not in (select top " + page + "  course_id from tb_score " + " where " + wh +
                    " and student_id = '" + Session["student_id"] +"')";
            }
            else
            {
                sql = sql + " and a.course_id not in (select top " + page + "  course_id from tb_score "+
                   " where student_id = '" + Session["student_id"] +  "')";
            }
            table1 = SqlServer.GetDataTable(sql);
        }
        catch (Exception ex)
        {
            Response.Write("-1");
            Response.End();
        }
        string a = Json.ToJson(table1, count);
        Response.Write(Json.ToJson(table1, count));
        Response.End();

    }


}
